package com.projectName.www.dao;

import com.projectName.www.po.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class User_dao {
    public int zhuce(Connection con,User user) throws SQLException {

        String sql="insert ignore into user (id,name,password,money) values(?,?,?,?) ";
        PreparedStatement p = con.prepareStatement(sql);
        if(user.getId()==0)
        {
            p.setInt(4,1000);
        }else p.setInt(4,0);
        p.setInt(1,user.getId());
        p.setString(2,user.getName());
        p.setString(3,user.getPassword());
       return p.executeUpdate();

    }

    public User login(Connection con,User user)throws Exception
    {
        User resultUser=null;
        String sql="select * from user where name=? and password=?";
        PreparedStatement p = con.prepareStatement(sql);
        p.setString(1,user.getName());
        p.setString(2,user.getPassword());
        ResultSet r = p.executeQuery();
        if(r.next())
        {
            resultUser=new User();
            resultUser.setId(r.getInt("id"));
            resultUser.setName(r.getString("name"));
            resultUser.setPassword(r.getString("password"));

        }
        return resultUser;
    }

}
